package managers;

import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
import org.w3c.dom.Document;

public class JsonHelper {
	public JSONObject fromXMLtoJson(String requestUrl) throws JSONException
	{
		JSONObject obj = new JSONObject();
		String output = "";
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	        DocumentBuilder db = dbf.newDocumentBuilder();
	        Document doc = db.parse(requestUrl);
	       
	        TransformerFactory tf = TransformerFactory.newInstance();
	        Transformer transformer = tf.newTransformer();
	        StringWriter writer = new StringWriter();
	        transformer.transform(new DOMSource(doc), new StreamResult(writer));
	        output = writer.getBuffer().toString().replaceAll("\n|\r", "");
		} catch (Exception e) {
            throw new RuntimeException(e);
        }
		obj = XML.toJSONObject(output);
		
		return obj;
	}
}
